package com.FH.organization.bpmanage;


import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/22.
 */
@Stateless
public class BPManageImpl extends AccessDB implements BPManagerDAO {


    @Override
    public ResultDTO getBPManageInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            //先查询数据库中是否存在字段
            String sqlexit = "select count(1) from bpmanage  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("table_name")) {
                sql_w += " AND table_name = '" + json.getString("table_name") + "'";
            }
            if (json.has("manage_id")) {
                sql_w += " AND manage_id = '" + json.getString("manage_id") + "'";
            }

            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<BPManageShowDTO> result = new ArrayList<BPManageShowDTO>();

            if (!existName.equals("0")) {
                String sql = " select manage_id,type,table_name,name,show_view from bpmanage where 1=1 " + sql_w + sql_page;
                //     String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sql);

                for (int i = 0; i < seresult.size(); i++) {

                    BPManageShowDTO bp = new BPManageShowDTO();

                    bp.setManage_id(seresult.get(i)[1]);
                    bp.setType(seresult.get(i)[2]);
                    bp.setTable_name(seresult.get(i)[3]);
                    bp.setName(seresult.get(i)[4]);
                    bp.setShow_view(seresult.get(i)[5]);
                    result.add(bp);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (Exception e) {
            e.printStackTrace();

        }

        return resultDTO;
    }

    @Override
    public ResultDTO updateBPManageInfo(BPManagePullDTO bpManagePullDTO) {

        ResultDTO resultDTO = new ResultDTO();

        String manage_id = bpManagePullDTO.getManage_id();
        if(manage_id!=null&&!"".equals(manage_id)){
            String update = "update bpmanage set show_view = " + bpManagePullDTO.getShow_view() + " where manage_id = '" + bpManagePullDTO.getManage_id() +"'";
            int result = addReportingTo(update);
            if (result!=0){
                resultDTO.setStr(bpManagePullDTO.getManage_id());
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.update_success);
            }else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.update_fail);
            }
        }else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }
}
